package cn.sundat.web;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.sundat.model.Company;
import cn.sundat.model.CompanyProduct;
import cn.sundat.model.PageBean;
import cn.sundat.model.Product;
import cn.sundat.model.ProductLink;
import cn.sundat.service.CompanyProductService;
import cn.sundat.service.CompanyService;
import cn.sundat.service.ProductLinkService;
import cn.sundat.service.ProductService;
import cn.sundat.utils.FastJsonUtils;
import cn.sundat.utils.UploadUtils;

public class CompanyProductAction extends ActionSupport implements ModelDriven<CompanyProduct>{
	@Resource(name="productService")
	private ProductService productService;
	//属性方法封装
	private String comp_id;
	public String getComp_id() {
		return comp_id;
	}

	public void setComp_id(String comp_id) {
		this.comp_id = comp_id;
	}
	
	private String pro_name;
	public String getPro_name() {
		return pro_name;
	}

	public void setPro_name(String pro_name) {
		this.pro_name = pro_name;
	}

	Integer currPage = 1;
	Integer pageSize = 8;
	
	public void setCurrPage(Integer currPage) {
		if(currPage==null) {
			currPage=1;
		}else {
			this.currPage=currPage;
		}
	}

	public void setPageSize(Integer pageSize) {
		if(pageSize==null) {
			pageSize=2;
		}else {
			this.pageSize=pageSize;
		}
	}
	
	/*
	 * 文件上传
	 */
	private File upload;
	private String uploadFileName;
	private String uploadContentType;

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	
	public String getUploadContentType() {
		return uploadContentType;
	}



	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	/*
	 * 注入
	 */
	@Resource(name="companyProductService")
	private CompanyProductService companyProductService;
	private CompanyProduct companyProduct = new CompanyProduct();
	
	@Resource(name="productLinkService")
	private ProductLinkService productLinkService;
	
	@Resource(name="companyService")
	private CompanyService companyService;
	
	@Override
	public CompanyProduct getModel() {
		return companyProduct;
	}
	

	/*
	 * 方法开始
	 */
	public String save() throws IOException {
		System.out.println(companyProduct);
		System.out.println("第一部分");
		//有产品描述上传
		if(uploadFileName!=null && uploadFileName.length()>0) {
			System.out.println("进来了没有");
			String realPath = ServletActionContext.getServletContext().getRealPath("/plan");
			//存储文件夹,以公司名字区分
			String comp_id = companyProduct.getCompany().getComp_id();
			Company c =  companyService.getById(comp_id);
			String companyFileName = UploadUtils.getCompanyFileName(c.getComp_name());
			File companyFile = new File(realPath+File.separator+companyFileName);
			if(!companyFile.exists() && !companyFile.isDirectory()) {
				companyFile.mkdir();
			}
			//文件位置
			String uuidName = UploadUtils.getUUIDName(uploadFileName);
			File newFile = new File(companyFile, uuidName);
			//将产品复制
			FileUtils.copyFile(upload, newFile);
			//设置file字段为存储的值
			companyProduct.setFile(companyFileName+File.separator+uuidName);
		}
		System.out.println("第三部分");
		//调用service保存，并且在多对一的多方设置了级联保存
		String link_id = companyProduct.getProductLink().getLink_id();
		//获取永久化productlink
		ProductLink link =  productLinkService.getById(link_id);
		//设置
		companyProduct.setProductLink(link);
		companyProductService.save(companyProduct);
		
		return NONE;
	}
	/**
	 * 根据公司名字查找有该产品的公司
	 * @return
	 */
	public String findByCName() {
		Company company = companyService.getById(comp_id);
		PageBean<CompanyProduct> pblist = companyProductService.findPBByCName(currPage,pageSize,company);
		String id = null;
		ActionContext.getContext().getValueStack().set("pblist", pblist);
		ServletActionContext.getRequest().setAttribute("company", company);
		return "list";
	}
	
	/**
	 * 根据产品的名字模糊查询所有该产品
	 * @return
	 */
	public String findByPName() {
		//1.根据产品名字查找到产品id
		Product p =  companyProductService.findIdByName(pro_name);
		if(p==null) {
			ServletActionContext.getRequest().setAttribute("pblist", null);
			return "namelist";
		}
		
		String pro_id = p.getPro_id();
		//2.根据comp_id查找产品列表
		PageBean<CompanyProduct> pblist = companyProductService.findByPId(currPage, pageSize, pro_id);
		System.out.println(pblist.getList().size());
		ServletActionContext.getRequest().setAttribute("pblist", pblist);		
		return "namelist";
	}
	
	public String delete() {
		//根据产品id查找到产品
		CompanyProduct cp = companyProductService.getById(companyProduct.getCp_id());
		//调用业务方法
		companyProductService.delete(cp);
		return NONE;
	}
	
	/**
	 * 根据id查找该产品
	 * @return
	 */
	public String findById() {
		CompanyProduct cp = companyProductService.getById(companyProduct.getCp_id());
		List<Product> plist = productService.findAll();
		//将list集合放入valuestack中的content
		ActionContext.getContext().put("plist", plist);
		//将cp放入valuestack的root中
		ActionContext.getContext().getValueStack().push(cp);
		//查询所有的联系人
		Company company = cp.getCompany();
		List<ProductLink> linklist = productLinkService.findAll(company);
		ServletActionContext.getRequest().setAttribute("linklist", linklist);
		/*String jsonString = FastJsonUtils.toJsonString(cp);
		HttpServletResponse response = ServletActionContext.getResponse();
		FastJsonUtils.write_json(response, jsonString);*/
		return "cpview";
	}
	
	/**
	 * 跟新公司产品
	 * @return
	 * @throws IOException 
	 */
	public String update() throws IOException {
		//原信息
		CompanyProduct cp = companyProductService.getById(companyProduct.getCp_id());
		//更新新信息
		String link_id = companyProduct.getProductLink().getLink_id();
		ProductLink link = productLinkService.getById(link_id);
		System.out.println("联系人id"+link_id);
		System.out.println("联系人姓名："+link);
//		ProductLink link = productLinkService.getById(companyProduct.getProductLink().getLink_id());
		cp.setProductLink(link);
		cp.setInfo(companyProduct.getInfo());
		//根据有没有上传，判断要不要更新文件存储信息
		if(uploadFileName!=null && uploadFileName.length()>0) {
			//重新上传了文件，删除旧文件，上传新文件
			String realPath = ServletActionContext.getServletContext().getRealPath("/plan");
			File file = new File(realPath, cp.getFile());
			if(file.exists()) {
				file.delete();
			}
			//上传新文件
			String companyFileName = UploadUtils.getCompanyFileName(cp.getCompany().getComp_name());
			File companyFile = new File(realPath, companyFileName);
			if(!companyFile.exists() && !companyFile.isDirectory()) {
				companyFile.mkdir();
			}
			String uuidName = UploadUtils.getUUIDName(uploadFileName);
			File newFile = new File(companyFile, uuidName);
			
			FileUtils.copyFile(upload, newFile);
			cp.setFile(companyFileName+File.separator+uuidName);
		}
		//调用业务方法
		companyProductService.update(cp);
		return NONE;
	}
	
}
